package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by niufei on 2017/4/8.
 *
 */
@Data
public class OtherPurchaseRecord implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -5677797412522959678L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 单据编号
     */
    private String number;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 购入日期
     */
    private Date date;

    /**
     * 采购名称
     */
    private String productName;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 单价
     */
    private Float price;

    /**
     * 总价
     */
    private Float totalPrice;

    /**
     * 经销商名称
     */
    private String dealerName;

    /**
     * 生产厂家、种畜场名称
     */
    private String manufacturer;

    /**
     *
     */
    private Map<String , Map<String , String>> batchNumberPhotoMap;

    /**
     * 批号照片
     */
    @JsonIgnore
    private String batchNumberPhoto;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 单据标志位 1：正常，-1：删除
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setBatchNumberPhoto(String batchNumberPhoto){
        this.batchNumberPhoto = batchNumberPhoto;

        if(Strings.isNullOrEmpty(batchNumberPhoto)){
            this.batchNumberPhotoMap = Maps.newHashMap();
        }else {
            try {
                this.batchNumberPhotoMap = objectMapper.readValue(batchNumberPhoto, new TypeReference<Map<String ,Map<String,String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setBatchNumberPhotoMap(Map<String , Map<String , String>> batchNumberPhotoMap){
        this.batchNumberPhotoMap = batchNumberPhotoMap;

        if(batchNumberPhotoMap == null || batchNumberPhotoMap.isEmpty()){
            this.batchNumberPhoto = null;
        }else {
            try {
                this.batchNumberPhoto = objectMapper.writeValueAsString(batchNumberPhotoMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }
}
